package com.stylefeng.guns.modular.item.util;
//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//

import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author: yyi
 * @date: 2018/9/27 10:55
 * @parameters:
 * @desc:
 **/
@Controller
@RequestMapping("/newsEditor")
public class FileUpLoadController {
    @Autowired
    private GunsProperties gunsProperties;
    /**
     * 上传图片
     */
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public Map upload(@RequestPart("file") MultipartFile picture) {
        Map map = new HashMap();
        map.put("errno",0);
        String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(picture.getOriginalFilename());
        try {
            String fileSavePath = gunsProperties.getFileUploadPath();
            picture.transferTo(new File(fileSavePath + pictureName));
            List list = new ArrayList();
            list.add("/upload/"+pictureName);
            map.put("data",list);
        } catch (Exception e) {
            throw new GunsException(BizExceptionEnum.UPLOAD_ERROR);
        }
        return map;
    }
    /***
     * 文件上传
     *
     * **/
    @RequestMapping(method = RequestMethod.POST,path="/uploadFile")
    @ResponseBody
    public String uploadFile(@RequestPart("file") MultipartFile file){
        String fileName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(file.getOriginalFilename());
        String fileSavePath = gunsProperties.getFileUploadPath();//服务器保存的路径
        System.out.println(fileSavePath);
        try {
            file.transferTo(new File(fileSavePath + fileName));
        } catch (IOException e) {
            throw new GunsException(BizExceptionEnum.UPLOAD_ERROR);
        }
        System.out.println(fileName);
        return "/upload/" + fileName;
    }
}
